package com.shsh.safemanage.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.shsh.safemanage.model.Keshi;
import com.shsh.safemanage.util.ValueUtil;

public class DepartmentKeshiDao {
	public List<Keshi> getKeshiList() {
		List<Keshi> keshiList = new ArrayList<Keshi>();
		
		String sql = "select * from keshi";
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			conn = ConnectionManager.getSafeManageConn();
			pstm = conn.prepareStatement(sql);
			
			ResultSet rs = pstm.executeQuery();
			while (rs.next()) {
				Keshi keshi = new Keshi();
				keshi.setId(ValueUtil.getLong(rs.getLong("ID")));
				keshi.setKeshiName(ValueUtil.getString(rs.getString("KESHI_NAME")));
				keshi.setKeshiID(ValueUtil.getInteger(rs.getInt("KESHI_ID")));
				keshi.setDepartmentID(ValueUtil.getInteger(rs.getInt("DEPARTMENT_ID")));
				
				keshiList.add(keshi);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}
		
		return keshiList;
	}
	
	public Keshi getKeshi(int departmentID, int keshiID) {
		String sql = "select * from keshi where KESHI_ID=? and DEPARTMENT_ID=?";
		
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			conn = ConnectionManager.getSafeManageConn();
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, departmentID);
			pstm.setInt(2, keshiID);
			
			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				Keshi keshi = new Keshi();
				keshi.setId(ValueUtil.getLong(rs.getLong("ID")));
				keshi.setKeshiName(ValueUtil.getString(rs.getString("KESHI_NAME")));
				keshi.setKeshiID(ValueUtil.getInteger(rs.getInt("KESHI_ID")));
				keshi.setDepartmentID(ValueUtil.getInteger(rs.getInt("DEPARTMENT_ID")));
				
				return keshi;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}
		
		return null;
	}
	
	public Keshi getKeshi(Long id) {
		String sql = "select * from keshi where ID=?";
		
		Connection conn = null;
		PreparedStatement pstm = null;
		try {
			conn = ConnectionManager.getSafeManageConn();
			pstm = conn.prepareStatement(sql);
			pstm.setLong(1, id);
			
			ResultSet rs = pstm.executeQuery();
			if (rs.next()) {
				Keshi keshi = new Keshi();
				keshi.setId(ValueUtil.getLong(rs.getLong("ID")));
				keshi.setKeshiName(ValueUtil.getString(rs.getString("KESHI_NAME")));
				keshi.setKeshiID(ValueUtil.getInteger(rs.getInt("KESHI_ID")));
				keshi.setDepartmentID(ValueUtil.getInteger(rs.getInt("DEPARTMENT_ID")));
				
				return keshi;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
			}
		}
		
		return null;
	}
}
